package com.ev.custom.service.impl;

import com.ev.apis.model.DsResultResponse;
import com.ev.custom.dao.DeviceCapacityDao;
import com.ev.custom.domain.DeviceCapacityDO;
import com.ev.custom.service.DeviceCapacityService;
import com.ev.custom.vo.DeviceCapacityPageParam;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class DeviceCapacityServiceImpl implements DeviceCapacityService {
    @Autowired
    private DeviceCapacityDao deviceCapacityDao;
    @Autowired
    private MessageSourceHandler messageSourceHandler;

    @Override
    public DeviceCapacityDO get(Long id) {
        return deviceCapacityDao.get(id);
    }

    @Override
    public List<DeviceCapacityDO> list(Map<String, Object> map) {
        return deviceCapacityDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return deviceCapacityDao.count(map);
    }

    @Override
    public int save(DeviceCapacityDO deviceCapacity) {
        return deviceCapacityDao.save(deviceCapacity);
    }

    @Override
    public int update(DeviceCapacityDO deviceCapacity) {
        return deviceCapacityDao.update(deviceCapacity);
    }

    @Override
    public int updateAll(DeviceCapacityDO deviceCapacity) {
        return deviceCapacityDao.updateAll(deviceCapacity);
    }

    @Override
    public int remove(Long id) {
        return deviceCapacityDao.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return deviceCapacityDao.batchRemove(ids);
    }

    @Override
    public R saveAndVerify(DeviceCapacityDO saveParam) {
        boolean isUpdate = saveParam.getId() != null;
        List<DeviceCapacityDO> byDeviceId = deviceCapacityDao.getByDeviceId(saveParam);
        if (byDeviceId.size() > 0) {
            return R.error(messageSourceHandler.getMessage("device.materiel.isExist", null));
        }

        if (isUpdate) {
            return deviceCapacityDao.update(saveParam) > 0 ? R.ok() : R.error();
        }
        return deviceCapacityDao.save(saveParam) > 0 ? R.ok() : R.error();
    }

    @Override
    public Map<String, Object> getById(Long id) {
        Map<String, Object> getById = deviceCapacityDao.getById(id);
        Map<String, Object> result = Maps.newHashMap();
        if (getById !=null){
            result.put("data",getById);
            return result;
        }
        return result;
    }

    @Override
    public Map<String, Object> pageList(DeviceCapacityPageParam pageParam) {
        Map<String, Object> result = Maps.newHashMap();
        List<Map<String, Object>> data = deviceCapacityDao.pageList(pageParam);
        if (data.size() > 0) {
            int total = deviceCapacityDao.pageCount(pageParam);
            result.put("data", new DsResultResponse(pageParam.getPageno(), pageParam.getPagesize(), total, data));
        }
        return result;
    }

}
